﻿using MediatR;
using SystemHYS.API.Application.command.ProductionRequisitionCommand;
using SystemHYS.Domian.DTO;
using SystemHYS.Infrstructre.Interface;
using SystemHYS.Infrstructre;
using Newtonsoft.Json;
using Share;
using static Share.ParseAPI;
using SystemHYS.Domian;
namespace SystemHYS.API.Application.commandler.ProductionRequisitionCommandler
{
    public class ProductionRequisitionModelCommandler : IRequestHandler<ProductionRequisitionModelCommand, ProductionRequisitionShowDTO>
    {
        IRawMaterialProductionRepository rawMaterialProductionRepository;
        IProductionRequisitionRepository productionRequisitionRepository;
        public SystemCoretext db;
        public ProductionRequisitionModelCommandler(IRawMaterialProductionRepository rawMaterialProductionRepository, IProductionRequisitionRepository productionRequisitionRepository, SystemCoretext db)
        {
            this.rawMaterialProductionRepository = rawMaterialProductionRepository;
            this.productionRequisitionRepository = productionRequisitionRepository;
            this.db = db;
        }
        public async Task<ProductionRequisitionShowDTO> Handle(ProductionRequisitionModelCommand request, CancellationToken cancellationToken)
        {
            var list = await GetRawMaterial();
            var rlist = await rawMaterialProductionRepository.GetAllAsnc();
            var plist= await productionRequisitionRepository.GetAllAsnc();
            var glist= (from a in plist select new ProductionRequisitionShowDTO
            {
                productionRequisitionId= a.productionRequisitionId,
                requisitionNumber= a.requisitionNumber,
                requisitionName= a.requisitionName,
                requisitionDate= a.requisitionDate,
                requisitionPerson= a.requisitionPerson,
                workOrderName= a.workOrderName,
                workOrderOrder=a.workOrderOrder,
                taskName = a.taskName,
                taskOrder = a.taskOrder,
                processRoute = a.processRoute,
                processName= a.processName,
                remark= a.remark,
                state= a.state,
                rawMaterialList=(from b in rlist 
                                 join c in list on b.RawMaterialId equals c.RawMaterialId 
                                 where b.productionRequisitionId==a.productionRequisitionId select new RawMaterialDTO
                                 {
                                     RawMaterialId = c.RawMaterialId,
                                     RawMaterialNumbering=c.RawMaterialNumbering,
                                     RawMaterialName=c.RawMaterialName,
                                     RawMaterialClassify = c.RawMaterialClassify,
                                     RawMaterialSize = c.RawMaterialSize,
                                     RawMaterialSpecModel=c.RawMaterialSpecModel,
                                     RawMaterialAttribute = c.RawMaterialAttribute,
                                     RawMaterialUnit=c.RawMaterialUnit,
                                     RawMaterialinventory = c.RawMaterialinventory,
                                     RawMaterialType = c.RawMaterialType,
                                     RawMaterilStatus=c.RawMaterilStatus,
                                     RawMaterialNumber=c.RawMaterialNumber,
                                     RawMaterialDay=c.RawMaterialDay,
                                     InventoryMaximums=c.InventoryMaximums,
                                     InentoryLower=c.InentoryLower,
                                     RawMateriallocation=c.RawMateriallocation,
                                     quantityReceived = b.quantityReceived
                                 }).ToList()
            }).FirstOrDefault();
            return glist; 
        }
        public async Task<List<RawMaterial>> GetRawMaterial(int pageindex = 1, int pagesize = 1000)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzMyIsInNpZCI6IjE4NjcwOTg1NDU4NTM2OTgwNDgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiIiwianRpIjoiMTYzZWM0ZTYtMTM0Yi00NmYwLWIxYmEtMDFhNjkwNWU0OWFjIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6Iui0uuiJs-W1qSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.xqb5lfaYGcOKr7lXwZY5ZnAN8gzcqYfc1z9dUKieVfI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://47.96.140.177:8000/RawApi/api/RawMaterials/RawMaterialShow?Pageindex=" + pageindex + "&Pagesize=" + pagesize;
            string postreonse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<RawMaterial>>(postreonse);
            return json.data.plist;
        }
    }
}
